package studentSystem;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Landing {
    public static void main(String[] args) {
        ArrayList<User> list1 = new ArrayList<>();
        landingMenu(list1);
    }

    public static void landingMenu(ArrayList<User> list) {
        System.out.println("----------欢迎来到学生管理系统----------");
        System.out.println("请选择操作(输入操作前的序号)");
        System.out.println("1、登录");
        System.out.println("2、注册");
        System.out.println("3、忘记密码");
        System.out.println("4、退出");
        System.out.println();
        System.out.println("您要选择的操作是：");

        Scanner sc = new Scanner(System.in);
        String i = sc.next();
        switch (i) {
            case "1":
                land(list);
                break;
            case "2":
                enrollName(list);
                break;
            case "3":
                forgetPassword(list);
                break;
            case "4":
                System.out.println("欢迎与您再次相遇！");
                System.exit(0);
                break;
            default:
                System.out.println();
                System.out.println("请输入一个正确的数字");
                landingMenu(list);
                break;
        }
    }

    public static void land(ArrayList<User> list) {
        System.out.println("您的选择是：登录");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您的用户名：");
        String name = sc.next();
//            遍历arraylist并返回索引，方便检测密码是否正确
        int index = containsName(list, name);
        if (index == -1) {
            System.out.println("该用户未注册，请先注册。");
            landingMenu(list);
        } else if (index >= 0) {
            boolean tf = false;
            while (!tf) {
                tf = captchaInspect();
            }
            User u = list.get(index);
            for (int i = 0; i < 3; i++) {
                System.out.println("请输入您的密码：");
                String password = sc.next();
                if(u.getPassword().equals(password)){
                    System.out.println("登录成功。");
                    /*感觉可以直接转换到Menu菜单，如果到main方法括号内该输入什么
                    如果到chooseMenu里面的话是不是将建立Student的集合放在这里建立*/
                    Menu mn = new Menu();
                    mn.startStudentSystem(list);
                }else{
                    System.out.println("密码输入错误。");
                    System.out.println("还有" + (2-i) + "次机会。");
                    if(i == 2){
                        System.exit(0);
                    }
                }
            }
        }
    }

    public static void enrollName(ArrayList<User> list) {
        System.out.println("您的选择是：注册");
//        新建一个User对象，每合法一个成员变量就set一个
        User u = new User();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您的用户名：");
        String userName = sc.next();
        for (int i = 0; i < list.size(); i++) {
            User u1 = list.get(i);
            if (u1.getUserName().equals(userName)) {
//                用户名唯一性判断
                System.out.println("用户名已存在，请重新输入。");
                enrollName(list);
            }
        }
        if (!stringLength(userName)) {
//                用户名长度判断
            System.out.println("用户名不规范，请重新输入。");
            enrollName(list);
        } else if (!nameNumberJudge(userName)) {
//                用户名数字判断
            System.out.println("用户名不规范，请重新输入。");
            enrollName(list);
        } else {
            u.setUserName(userName);
//                转入输入密码的方法
            enrollPassword(list, u);
        }
    }

    public static void enrollPassword(ArrayList<User> list, User u) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您的密码：");
        String password = sc.next();
        System.out.println("请再次确认密码：");
        String confirmPassword = sc.next();
        if (password.equals(confirmPassword)) {
//            两次密码一致判断
            u.setPassword(password);
//            转入输入身份证号
            enrollIDNumber(list, u);
        } else {
            System.out.println();
            System.out.println("密码不一致,请重新输入");
            enrollPassword(list, u);
        }
    }

    public static void enrollIDNumber(ArrayList<User> list, User u) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您的身份证号：");
        String IDNumber = sc.next();
        if (IDNumber.length() != 18) {
//            身份证长度判断
            System.out.println();
            System.out.println("身份证长度应该为18位。");
            System.out.println("身份证不规范，请重新输入。");
            enrollIDNumber(list, u);
        } else if (!notZeroJudge(IDNumber)) {
//            身份证开头非0判断
            System.out.println("身份证不规范，请重新输入。");
            enrollIDNumber(list, u);
        } else if (!IDJudge(IDNumber)) {
//            身份证前十七位是数字，末位是数字、X或x
            System.out.println("身份证不规范，请重新输入。");
            enrollIDNumber(list, u);
        } else {
            u.setIDNumber(IDNumber);
            enrollPhoneNumber(list, u);
        }
    }

    public static void enrollPhoneNumber(ArrayList<User> list, User u) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您的手机号：");
        String phone = sc.next();
        if (phone.length() != 11) {
//            手机号长度为11位
            System.out.println();
            System.out.println("手机号长度应该为11位。");
            System.out.println("手机号不规范，请重新输入。");
            enrollPhoneNumber(list, u);
        } else if (!notZeroJudge(phone)) {
//            手机号开头非0判断
            System.out.println("手机号不规范，请重新输入。");
            enrollPhoneNumber(list, u);
        } else if (!phoneJudge(phone)) {
//            手机号都是数字
            System.out.println("手机号不规范，请重新输入。");
            enrollPhoneNumber(list, u);
        } else {
            u.setPhoneNumber(phone);
            list.add(u);
            System.out.println();
            System.out.println("注册成功。");
            landingMenu(list);
        }
    }

    public static void forgetPassword(ArrayList<User> list) {
        System.out.println("您的选择是：忘记密码");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您的用户名：");
        String name = sc.next();
//        遍历搜索用户名是否存在并返回索引
        int index = containsName(list, name);
        if(index == -1){
            System.out.println("该用户未注册，请先注册。");
            landingMenu(list);
        }else if(index >= 0){
            User u = list.get(index);
            System.out.println("请输入您的身份证号：");
            String Id = sc.next();
            if(!u.getIDNumber().equalsIgnoreCase(Id)){
                System.out.println("账号信息不匹配，修改失败。");
                landingMenu(list);
            }else{
                System.out.println("请输入您的手机号：");
                String phone = sc.next();
                if(!u.getPhoneNumber().equals(phone)){
                    System.out.println("账号信息不匹配，修改失败。");
                    landingMenu(list);
                }else{
                    System.out.println("您的密码要修改为：");
                    String password = sc.next();
                    System.out.println("请再次输入密码：");
                    String check = sc.next();
                    if(password.equals(check)){
                        u.setPassword(password);
                        list.set(index,u);
                        System.out.println("修改成功。");
                        landingMenu(list);
                    }else{
                        System.out.println("两次密码输入不一致，修改失败。");
                        landingMenu(list);
                    }
                }
            }
        }
    }

    public static boolean stringLength(String name) {
        if (name.length() >= 3 && name.length() <= 15) {
            return true;
        } else {
            System.out.println();
            System.out.println("用户名应该在3~15长度内。");
            return false;
        }
    }

    public static boolean nameNumberJudge(String name) {
        int num = 0;
        int letter = 0;
        for (int i = 0; i < name.length(); i++) {
            char a = name.charAt(i);
            if (a <= '9' && a >= '0') {
                num++;
            } else if ((a >= 'A' && a <= 'Z') || (a >= 'a' && a <= 'z')) {
                letter++;
            }
        }
        if (num == 0 || letter == 0) {
            System.out.println();
            System.out.println("用户名必须由字母和数字组成。");
            return false;
        } else {
            return true;
        }
    }

    public static boolean notZeroJudge(String st) {
        if (st.charAt(0) == '0') {
            System.out.println();
            System.out.println("不能以0为开头。");
            return false;
        } else {
            return true;
        }
    }

    public static boolean IDJudge(String ID) {
        for (int i = 0; i < ID.length() - 1; i++) {
            char IDPlace = ID.charAt(i);
            if (!(IDPlace >= '0' && IDPlace <= '9')) {
                System.out.println();
                System.out.println("身份证前17位应该为数字。");
                return false;
            }
        }
        char IDLastPlace = ID.charAt(ID.length() - 1);
        if (IDLastPlace != 'X' && IDLastPlace != 'x' && !(IDLastPlace >= '0' && IDLastPlace <= '9')) {
            System.out.println();
            System.out.println("身份证最后一位应该为数字或者大小写x。");
            return false;
        }
        return true;
    }

    public static boolean phoneJudge(String phone) {
        for (int i = 0; i < phone.length(); i++) {
            char phonePlace = phone.charAt(i);
            if (!(phonePlace >= '0' && phonePlace <= '9')) {
                System.out.println();
                System.out.println("手机号应都为数字。");
                return false;
            }
        }
        return true;
    }

    public static int containsName(ArrayList<User> list, String name) {
        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            if (u.getUserName().equals(name)) {
                return i;
            }
        }
        return -1;
    }

    public static boolean captchaInspect() {
        System.out.print("您的验证码为：");
//        弄一个数组存储所有字母
        char[] letter = new char[52];
        for (int i = 0; i < letter.length; i++) {
            if (i <= 25) {
                letter[i] = (char) (97 + i);
            } else {
                letter[i] = (char) (65 + i - 26);
            }
        }
//        生成验证码
        Random r = new Random();
        int index = r.nextInt(0, 5);
        String captcha = "";
        for (int i = 0; i < 5; i++) {
            if (i == index) {
                int getNumber = r.nextInt(0, 10);
                captcha = captcha + getNumber;
                continue;
            }
            int getLetter = r.nextInt(0, 52);
            captcha = captcha + letter[getLetter];
        }
        System.out.println(captcha);
//        输入验证码
        System.out.println("请输入您的验证码：");
        Scanner sc = new Scanner(System.in);
        String inspect = sc.next();
        if (inspect.equalsIgnoreCase(captcha)) {
            return true;
        } else {
            System.out.println("验证码错误，请重新输入。");
            return false;
        }
    }
}
